All right.
We are quoting now the S&P 500 the trend triangle and first of all we import Pena's name pi and seaborne
and also Matt Platt lip.
And we will use the Seabourn style and then we are working with the S&P 500 CSP file.
So we import the file and of course we set the date column as being the day time index.
And we only use them the column date and close.
So we are working with the closing prices.
And again he as a side note.
So we only capture here the price return and not the total return because we do not include here the
dividends.
So let's import here and let's have a look.
So this is nothing new.
So our dataset starts here in the year nineteen hundred seventy.
And for our purposes we are only interested that in the last the 30 year period.
So from nineteen hundred eighty nine today to two thousand and eighteen.
So now here our data frame starts at the very last day of the year nineteen hundred eighty eight and
actually follow our return triangle we want to work with add new price data and therefore we will sample
the S&P 500 the data frame to endure.
And we take the very last trading price of a particular year.
So let's throw this here and uh we save the IRI sample data frame and the variable annual.
So these are actually the annual prices.
So in nineteen hundred eighty eight eighty nine and so on.
And then as a next step we create annual lucky returns.
So we create the column return and we take the close column and divide each element in the cross column
by the immediately preceding price and then we take the logarithm.
So let's do this here and we already know that we produce at least one and a value.
So apparently there might not be any return for the year nineteen hundred eighty eight
so let's have a look here.
So in our data frame starts in the year nineteen hundred eighty nine.
And here we have the very last trading price and here we have the end you are lucky returns.
So for example the annual lock return for the year nineteen hundred eighty nine was a 24 percent and
in total we have annual returns from nineteen hundred eighty nine to two thousand and eighteen.
So here we have a negative lock return of minus seven point two percent.
So in total we have a period of 30 years but we can also make this more explicit by calling here the
index and the calculate actually the size of our index.
And uh we actually save the size and the variable yes so it should be 30.
So 30 years and then we want to create the rolling statistics for investment periods from one year to
30 years.
And as you might recall in the last video.
So the 30 year period started on the left hand side of our chart and the one year period on the right
hand side and therefore first of all we created a list with a list comprehension and the list should
include the integer starting from 30 until 1 inclusive.
And we can do the CSO year for year in range uh 30 to zero exclusive with the steps left minus 1.
So let's have a look at the list of Windows.
So here we have the integers from 38 uh 1.
And now we will create the 30 columns for our annual data frame.
So for each period starting with 30 years until one year we are actually creating a column so for year
and Windows starting here with 30 years until one year we create the column for the annual data frame
30 years twenty nine years until one year and we actually take the annual lock return column and calculate
uh the rolling mean.
So this is a for loop and in the very first iteration we are starting with uh 30 here.
So an investment period of 30.
And we create the column 30 years then we are calculating the rolling mean return with a rolling window
of 30 years.
So let's have a look here.
So let's uh execute here.
The for loop and let's have a look at our data frame and Uralla.
So here we can see that we successfully EDIT Actually 30 columns.
So here we have the close column the return column and then give you a further rolling mean for the
investment period of 30 years.
So no surprise that we have uh any end values here.
And as we in total consider only the period starting from nineteen hundred eighty nine.
So we have you in the column 30 years only one data point.
So here for the period from nineteen hundred eighty nine to two thousand and eighteen then we have a
fun investment period of twenty nine years.
We have two data points or two or rolling statistics.
And if we go to the right here then here we have um the one year rolling average return.
So these are actually the respective lock returns and the particular year.
So starting from nineteen hundred eighty nine to two thousand and eighteen and we are actually almost
finished here.
So the only thing we have to do here we have to delete the columns close and return.
So we are happy here with the 30 columns here both of the rolling statistics and therefore we drop the
columns close and return and we save the new data frame and the rabbit triangle.
So that's with us.
And that's again never look so here we have on the left hand side on the y axis.
The final year of investment and on the x axis here the investment periods.
So we have successfully rearranged here and aggregated our data and now we can visualize our data and
create a return triangle.
And uh we can do this year with a seaborne heat map.
So first of all we defined that you want to have a fixed size of 50 40.
So this is a quite large graph here actually.
Then we increase the funds go to one point eight and then finally we pass our triangular data frame
to the S.A. heat map.
So let's run the other side and let's have a look
so here we have our return triangle and this looks quite familiar actually for us.
And uh let's go also here to the decoding details so we can actually see in each cell the average are
the mean lock return and by passing through to the end of parameter we are actually adding here the
numbers.
And then next we have the format parameter and here we can define that.
Our numbers should be in percent with the one decimal and as color map we have to find your red yellow
green.
So we can see here that the negative returns are red and the positive returns are green.
And actually uh zero returns are kind of low.
And with the women and we max we can also define the scale of our color map.
So starting with minus 10 percent.
So minus 10 percent is the dark red.
And actually plus 15 percent is dark green and the center should be actually at 0 percent.
So here we have a yellow and actually here we have another met public function party the tick parents
and we define here that for the y axis we set a label right to true.
So we can see here that we have few on the left hand side and also on the right hand side here.
The y axis labels and the free pass here for it to the label right
then we only f on the left hand side here the labels but not on the right hand side.
And it might be more comfortable to have these labels also on the right hand side and therefore we pass
the a true.
So this is our final return triangle for the S&P 500 index.
And as we have seen here.
So we only need the some few lines of code.
So this is pretty amazing and I hope to see you also in the next video by.